//
//  EmotionCollectionViewCell.swift
//  新浪微博项目
//
//  Created by 王炯 on 16/7/30.
//  Copyright © 2016年 王炯. All rights reserved.
//

import UIKit

class EmotionCollectionViewCell: UICollectionViewCell
{
    
    //懒加载button按钮数组，用于则cell上显示button
    private lazy var buttons:[EmotionButton] = [EmotionButton]()
    
    
    //懒加载一维EmotionModel数组，用于设置当前cell上所有的button的图片和文字
    var EmotionModelArray:[EmotionModel]?{
        

        didSet
        {
            //1-先把所有的button隐藏
            for b in buttons
            {
                b.hidden = true
            }
            
            //2-遍历EmotionModelArray数组
            //注意，一定是遍历EmotionModelArray数组来设置button数组，而不能遍历button数组
            for (i,model) in EmotionModelArray!.enumerate()
            {
                //用i取button
                let button = buttons[i]
                
                //设置model
                button.model = model
                
                //显示button
                button.hidden = false
                
            }
        
        }
        
        
    }
    
    //在最后一个的位置添加删除按钮
    private lazy var deleteButton:UIButton = {
       
        
        let button = UIButton()
        
        
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        
        //添加删除按钮的触发事件
        button.addTarget(self, action: #selector(EmotionCollectionViewCell.DeleteClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return button
    }()
    
    
    //删除按钮的触发事件
    func DeleteClick()
    {
        //发送通知给主AddWordController
        NSNotificationCenter.defaultCenter().postNotificationName("DeleteText", object: nil)
    }
    
    //表情按钮的触发事件
    func EmotionClick(button:EmotionButton)
    {
        //发送通知和button的model参数给主AddWordController
        NSNotificationCenter.defaultCenter().postNotificationName("AddEmotionText", object: button.model)
        
    }

    override init(frame: CGRect)
    {
        
        super.init(frame: frame)
        
        //布局界面，在界面上添加image
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    //布局界面
    func setUpUI()
    {
        
        //设置20个button
        self.setUpButtons()
        
        //添加删除按钮
        contentView.addSubview(self.deleteButton)
        
       
    }
    
    //当cell的frame已经确定好了之后在
    override func layoutSubviews()
    {
        //在cell上添加20个button
        //(1)button的宽
        let buttonW = (self.frame.width - 10) / CGFloat(EmoticonMaxCol)
        
        //(2)button的高
        let buttonH = (self.frame.height - 20) / CGFloat(EmoticonMaxRow)
        
        
        //遍历button,设置button的frame
        for (i,value) in buttons.enumerate()
        {
            value.frame = CGRect(x: CGFloat(i % EmoticonMaxCol) * buttonW + 5, y: CGFloat(i / EmoticonMaxCol) * buttonH , width: buttonW, height: buttonH)
        }
        
        
        //设置删除按钮的frame
        self.deleteButton.frame = CGRect(x: CGFloat(EmoticonMaxCount % EmoticonMaxCol) * buttonW + 5, y: CGFloat(EmoticonMaxCount / EmoticonMaxCol) * buttonH , width: buttonW, height: buttonH)
    }
    
    
    //在cell上添加20个button
    func setUpButtons()
    {
        
        //(3)循环添加20个按钮
        for _ in 0..<EmoticonMaxCount
        {
            let button:EmotionButton = EmotionButton()
            
            button.titleLabel?.font = UIFont.systemFontOfSize(30)
            
            
            //给每一个表情包按钮添加触发事件
            button.addTarget(self, action: #selector(EmotionCollectionViewCell.EmotionClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
            
            
            contentView.addSubview(button)
            
            buttons.append(button)
        }
        
    }
    
    
}
